package edu.rpi.cs.webgraph.graphfragment;

import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.DirectedSparseGraph;
import org.apache.commons.collections15.Factory;

/**
 * Author: josh
 * Date: Apr 2, 2009
 * Time: 9:00:17 PM
 */
public class TestingFramework {
    public interface TimedTask {
        void run(int param);
    }

    public static void main(final String[] args) {
        TimedTask bruteForceAlgo = new TimedTask() {

            public void run(int param) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        };

        TimedTask improvedAlgo = new TimedTask() {

            public void run(int param) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        };

        TimedTask[] tasks = {bruteForceAlgo, improvedAlgo};

        int min = 0;
        int max = 100;
        int increment = 10;
        long timeoutSeconds = 10;
        long[][] results = testSeries(min, max, increment, 1000 * timeoutSeconds, tasks);

        StringBuilder sb = new StringBuilder();

        for (long[] r : results) {
            for (long v : r) {
                sb.append("\t");
                sb.append(v);
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }

    // TODO: use timeout
    private static long[][] testSeries(final int min,
                                       final int max,
                                       final int increment,
                                       final long timeout,
                                       final TimedTask... tasks) {
        int iterations = (max - min) / increment;
        long[][] results = new long[tasks.length][iterations];

        for (int i = 0; i < iterations; i++) {
            int v = min + (i * increment);

            int j = 0;
            for (TimedTask task : tasks) {
                long before = System.currentTimeMillis();
                boolean timedOut = false;

                task.run(v);

                long after = System.currentTimeMillis();
                long duration = after - before;

                results[j][i] = duration;
                j++;
            }
        }

        return results;
    }

    public static Factory<Graph<Integer, String>> typicalGraphFactory() {
        Factory<Graph<Integer, String>> graphFactory
                = new Factory<Graph<Integer, String>>() {

            public Graph<Integer, String> create() {
                return new DirectedSparseGraph<Integer, String>();
            }
        };

        return graphFactory;
    }
}
